<?php

namespace app\api\controller;

use app\api\model\WxappPage;
use app\common\model\Areas;
use think\Console;
use think\Request;

/**
 * 页面控制器
 * Class Index
 * @package app\api\controller
 */
class Page extends Controller
{
    /**
     * 页面数据
     * @param null $page_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($page_id = null)
    {
        // 页面元素
        $data = WxappPage::getPageData($this->getUser(false), $page_id);
        return $this->renderSuccess($data);
    }

    // 获取省
    public function getProvinces()
    {
        $Brand = $this->request->param('Brand');
        $Brand_low = Areas::field('id,name,en_name,bg_color,text_color')->where(['type'=>0,'name'=>$Brand])->find();


        // 页面元素
        $data = Areas::field('id,name,en_name,bg_color,text_color')->where(['type'=>1,'pid'=>$Brand_low['id']])->order('weight','asc')->select();
        return $this->renderSuccess($data);
    }

    // 获取省
    public function getCitys(Request $request)
    {
        // 页面元素
        $data = Areas::field('id,name,en_name,bg_color,text_color')->where('pid', $request->post('pid'))->select();
        foreach ($data as &$item) {
            $childs = Areas::field('id,name,en_name,bg_color,text_color')
                ->with(['joins' => function($query) {
                    $query->field('id,area_id')->where('is_pay', 1);
                }])
                ->where('pid', $item->id)->select();
            foreach ($childs as &$child) {
                $child->is_join = !empty($child->joins) ? 1 : 0;
            }
            $item->childs =$childs;
        }
        return $this->renderSuccess($data);
    }

}
